<!--
 * @Author: Bobby
 * @Date: 2021-12-30 10:01:42
 * @LastEditTime: 2022-03-23 15:02:32
 * @LastEditors: Bobby
 * @Description:
 * @FilePath: \wms-v2\src\ve\elements\basic\uiForm\uiForm.vue
 * CIMC-V2
-->
<template>
  <div
    :id="hasPreview ? info._id : `diy_${info._id}`"
    ref="chart"
    class="ui-line-chart h-full"
    :style="{
      width: '100%',
    }"
  ></div>
</template>
<script>
  import { defineComponent } from 'vue'
  import { UiMixin, RefsInjectMixin, ChartMixin } from '@ve/mixin'
  import cloneDeep from 'lodash/cloneDeep'
  import { barOption, moreTypeOptions } from './chartOptionJson.js'
  import * as echarts from 'echarts'
  export default defineComponent({
    name: 'UiFunnelChart',
    components: {},
    mixins: [UiMixin, RefsInjectMixin, ChartMixin],
    inject: {
      hasPreview: { default: false },
    },
    props: {
      info: {
        required: true,
        type: Object,
        default: function () {
          return {}
        },
      },
    },
    data() {
      return {}
    },
    watch: {
      'info.extension': {
        handler() {
          this.$nextTick(() => {
            this.initChart()
          })
        },
        deep: true,
      },
      getSelectModule: {
        handler(val, oldVal) {
          if (!oldVal.extension) {
            if (barOption[this.info.chartTypeValue]) {
              this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = false)
            } else {
              this.getSelectModule.extension && this.getSelectModule.extension.series && (this.getSelectModule.extension.series.isShowBtn = true)
            }
          }
        },
        deep: true,
      },
    },
    created() {},
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    methods: {
      initChart() {
        let dataObj = [
          { value: 60, name: 'Visit' },
          { value: 40, name: 'Inquiry' },
          { value: 20, name: 'Order' },
          { value: 80, name: 'Click' },
          { value: 100, name: 'Show' },
        ]
        let str = 'abcdefghijklmnopqrstuvwxyz'
        // const moduleId = this.hasPreview ? this.info._id : `diy_${this.info._id}`
        let chartDom = this.$refs.chart
        let myChart = echarts.init(chartDom)
        let data = cloneDeep(this.info.extension)
        let option = this.formatterOption(data)
        let barOptions = cloneDeep(barOption)
        let series = barOptions[this.info.chartTypeValue] || moreTypeOptions[this.info.chartTypeValue]
        if (series) {
          option = Object.assign(option, series(dataObj))
        }
        option.color = option.color.map((item) => item.v)
        option.series.map((item, index) => {
          if (item.showID) {
            item.name = str.slice(index, index + 1)
          }
          return item
        })
        console.log(option)
        Object.defineProperty
        option && myChart.setOption(cloneDeep(option), true)
      },
    },
  })
</script>
<style lang="scss">
  .uiFunnelChart {
    width: 100%;
    height: 100%;
  }
</style>
